<?php


namespace app\admin\controller;

use DI\Annotation\Inject;
use support\Request;
use app\admin\model\Attachment as Model;

class Attachment extends BaseController
{
    protected $preViewName = 'attachment';

    /**
     * @var \app\admin\service\Attachment
     * @Inject()
     */
    protected $server = null;

    public function index(Request $request)
    {
        if ($request->expectsJson()) {
            $list = Model::query()->orderByDesc('created_at')->forPage($request->get('page', 1))->get();
            $count = Model::query()->count();
            $this->success("获取成功", [
                'list' => $list,
                'total' => $count
            ]);
        }
        return $this->view('index');
    }

    public function select(Request $request)
    {
        if ($request->expectsJson()) {
            $list = Model::query()->orderByDesc('created_at')->forPage($request->get('page', 1))->get();
            $count = Model::query()->count();
            $this->success("获取成功", [
                'list' => $list,
                'total' => $count
            ]);
        }
        return $this->view('select');
    }

    public function upload(Request $request)
    {
        $file = $request->file('file');
        $path = $this->server->init($file)->upload();
        $this->success("上传成功", [
            'path' => $path
        ]);
    }
}
